package com.xx.springboot.test.RabbitMQ.simple;

import com.rabbitmq.client.*;
import com.xx.springboot.test.RabbitMQ.RabbitMQUtil;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * decr:
 * create by Xiaoxi
 * email:xiaoxiunique@qq.com
 * phone:13541863172
 * date:2018/8/30/030 10:32
 * project_name:SpringBoot-MybatisPlus
 * package_name:com.xx.springboot.test.RabbitMQ.simple
 */
public class Consumer {
    private static final String QUEUE_NAME = "test_simple_queue";

    public static void main(String[] args) throws IOException, TimeoutException {
        /**
         * 创建连接
         */
        Connection connection = RabbitMQUtil.getConnection();

        // 创建通道
        Channel channel = connection.createChannel();

        // 队列声明
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);

        // 定义消费者
        DefaultConsumer consumer = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                String msg = new String(body, "utf-8");
                System.out.println(msg);
            }
        };

        // 监听 队列
        channel.basicConsume(QUEUE_NAME, true, consumer);
    }
}
